/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaModificarEliminar.java
 *
 * Created on May 4, 2012, 2:22:46 PM
 */

package programita;

import busquedas.Busquedas;
import busquedas.CodificarCursos;
import busquedas.Eliminar;
import busquedas.Validador;
import busquedas.Validador.miExcepcionEntrada;
import busquedas.Validador.miExcepcionLista;


import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import programita.Alumno;

/**
 *
 * @author educacionit
 */
public class VentanaModificarEliminar extends javax.swing.JFrame {
    DefaultListModel modelo= new DefaultListModel();
    /** Creates new form VentanaModificarEliminar */
    public VentanaModificarEliminar() {
        initComponents();
        jList.setModel(modelo);
        cargarMarcas();
    }

    public void cargarMarcas(){


            comboCurso.addItem("Seleccione..");
            comboCurso.addItem("Jardin");
            comboCurso.addItem("Primaria");
            comboCurso.addItem("Ingles");
            comboCurso.addItem("Sala Recup");
            comboDivision.addItem("Seleccione..");
           
   
            

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboCurso = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        textNombre = new javax.swing.JTextField();
        textApellido = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        comboDivision = new javax.swing.JComboBox();
        labelGrado = new javax.swing.JLabel();
        comboDivisionDelCurso = new javax.swing.JComboBox();
        LabelDivision = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nombre");

        jLabel2.setText("Apellido");

        comboCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCursoActionPerformed(evt);
            }
        });

        jLabel3.setText("Curso");

        jScrollPane1.setViewportView(jList);

        jButton1.setText("Modificar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Atras");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Buscar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        labelGrado.setText("Nivel");

        comboDivisionDelCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboDivisionDelCursoActionPerformed(evt);
            }
        });

        LabelDivision.setText("Division");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 65, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(labelGrado))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(textApellido, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(textApellido, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
                                    .addComponent(comboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(comboDivision, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(29, 29, 29)
                                        .addComponent(LabelDivision)
                                        .addGap(18, 18, 18)
                                        .addComponent(comboDivisionDelCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(textNombre))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))))))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelGrado)
                    .addComponent(comboDivision, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboDivisionDelCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LabelDivision))
                .addGap(18, 18, 18)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void agregarAlaLista(ArrayList<Alumno> listaA,DefaultListModel listaLog){
        for(int i=0;i<listaA.size();i++){
           listaLog.addElement(listaA.get(i));
       }
    }
    
    public boolean validarBusqCurso(){
        boolean eligio=false;
        if(comboCurso.getSelectedItem().equals("Seleccione..") || (comboCurso.getSelectedItem().equals("SalaRecu"))){
            System.out.println("No eligio nada");
        }else{
            if(comboDivision.getSelectedItem().equals("Seleccione..")){
                JOptionPane.showMessageDialog(this,"Debe seleccionar un Campo Nivel para realizar una busqueda por Curso");
                System.out.println("NO ELIGIO Nivel");
            }else{
                 
                if(comboDivisionDelCurso.getSelectedItem().equals("Seleccione..")){
                 JOptionPane.showMessageDialog(this,"Debe seleccionar una Division para realizar una busqueda por Curso");
                 System.out.println("FALTA PONER LA DIVISION");
                }else{
                    
                    System.out.println("HIZO UNA ELECCION");
                    eligio=true;
                    }
                }

            }     
         if(comboCurso.getSelectedItem().equals("SalaRecu")){
            eligio=true;
            System.out.println("HIZO UNA ELECCION DE RECUPERATORIO");
         }
        
     return eligio;
    }
    public ArrayList<Alumno> BusquedaCurso(){
        ArrayList<Alumno> alumnos=new ArrayList<Alumno>();
        try{
        int curso;
        Busquedas busq=new Busquedas();
        
            if(comboCurso.getSelectedItem().equals("Sala Recup")){
                curso=50;
                alumnos=busq.buscarCursoSalaRecup(50);
                System.out.println("Realizo la busqueda");
            }else if(comboCurso.getSelectedItem().equals("Ingles")){
                 curso=CodificarCursos.Codificar((String)comboCurso.getSelectedItem(), (String)comboDivision.getSelectedItem());
                 alumnos=busq.buscarCursoIngles(curso,(String)comboDivisionDelCurso.getSelectedItem());
                 System.out.println("Realizo la busqueda con numeros"+curso+(String)comboDivisionDelCurso.getSelectedItem());
            } else{
                  curso=CodificarCursos.Codificar((String)comboCurso.getSelectedItem(), (String)comboDivision.getSelectedItem());
                 alumnos=busq.buscarCurso(curso,(String)comboDivisionDelCurso.getSelectedItem());
                 System.out.println("Realizo la busqueda con numeros"+curso+(String)comboDivisionDelCurso.getSelectedItem());
            }
        
        }catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "No encontro nada");
        }finally{
            return alumnos;
        }
        
    }


    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            //falta que valide por si no busca nada!!
            modelo.clear();
            String nombre=textNombre.getText().toUpperCase();
            String apellido=textApellido.getText().toUpperCase();
            Validador.validadorEntradas(nombre, apellido,(String) comboCurso.getSelectedItem());
            Boolean noeligioNombre=(nombre.equals(""));
            Boolean noeligioApellido=(apellido.equals(""));
            Boolean eligioCurso=validarBusqCurso();
            Busquedas busq=new Busquedas();
            ArrayList<Alumno> alumnos=new ArrayList<Alumno>();
            

            if(!noeligioNombre & !noeligioApellido & eligioCurso){
                ArrayList<Alumno> ListaInt ;
                ArrayList<Alumno> ListaFin;
                alumnos = busq.buscarNombre(nombre);
                ArrayList<Alumno> a2 ;
                a2 = busq.buscarApellido(apellido);
                ArrayList<Alumno> a3 ;
                a3 = BusquedaCurso();
                ListaInt=Alumno.combinarL(alumnos, a2);
                ListaFin=Alumno.combinarL(ListaInt, a3);
                agregarAlaLista(ListaFin, modelo);
            }else if(!noeligioNombre & !noeligioApellido){
                ArrayList<Alumno> ListaInt ;
                alumnos = busq.buscarNombre(nombre);
                ArrayList<Alumno> a2;
                a2 = busq.buscarApellido(apellido);
                ListaInt=Alumno.combinarL(alumnos, a2);
                agregarAlaLista(ListaInt, modelo);
                }else if(!noeligioNombre & eligioCurso){
                    ArrayList<Alumno> ListaInt ;
                    alumnos = busq.buscarNombre(nombre);
                    ArrayList<Alumno> a3;
                    a3 = BusquedaCurso();
                    ListaInt=Alumno.combinarL(a3, alumnos);
                    agregarAlaLista(ListaInt, modelo);
                    }else if(!noeligioApellido& eligioCurso){
                        ArrayList<Alumno> ListaInt ;
                        ArrayList<Alumno> a2 ;
                        a2 = busq.buscarApellido(apellido);
                        ArrayList<Alumno> a3 ;
                        a3 = BusquedaCurso();
                        ListaInt=Alumno.combinarL(a2, a3);
                        agregarAlaLista(ListaInt, modelo);
                        }else if(!noeligioApellido){
                            System.out.println("Vamos a buscar por apellido:"+apellido);
                            alumnos=busq.buscarApellido(apellido);
                            agregarAlaLista(alumnos, modelo);

                            }else if(!noeligioNombre){
                                alumnos=busq.buscarNombre(nombre);
                                agregarAlaLista(alumnos, modelo);
                                }else{
                                    //eligiounCurso
                                System.out.println("Vamos a buscar por curso");
                                alumnos=BusquedaCurso();
                                agregarAlaLista(alumnos, modelo);

                                }

            
            //String nombre=textNombre.getText().toUpperCase();FUNCA
           // System.out.println(nombre);FUNCA
            //ArrayList alumnos2=busq.buscarNombre(nombre,"Perez");FUNCA
            //     agregarAlaLista(alumnos, modelo);
    //     ArrayList alumnos=busq.buscarCursoJardinyPrimaria(23,"A","1");
    //     agregarAlaLista(alumnos, modelo);
        } catch (miExcepcionEntrada ex) {
            Logger.getLogger(VentanaModificarEliminar.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    
    private void comboCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCursoActionPerformed
        
        String seleccion=(String)comboCurso.getSelectedItem(); 
         System.out.println("seleccion:"+seleccion);
                if(seleccion.equals("Jardin")){
                comboDivision.removeAllItems();
                comboDivisionDelCurso.removeAllItems();
                comboDivisionDelCurso.setVisible(true);
                comboDivision.setVisible(true);
                labelGrado.setVisible(true);
                LabelDivision.setVisible(true);
                comboDivisionDelCurso.addItem("Seleccione..");
                comboDivision.addItem("Seleccione..");
                comboDivision.addItem("Sala Roja");
                comboDivision.addItem("Sala Verde");
                comboDivision.addItem("Sala Azul");
                comboDivisionDelCurso.addItem("A");
                comboDivisionDelCurso.addItem("B");


            }else if(seleccion.equals("Primaria")){
                    
                    comboDivision.removeAllItems();
                    comboDivisionDelCurso.removeAllItems();
                    comboDivisionDelCurso.setVisible(true);
                    comboDivision.setVisible(true);
                    comboDivisionDelCurso.addItem("Seleccione..");
                    comboDivision.addItem("Seleccione..");
                    comboDivision.addItem("Primer Grado");
                    comboDivision.addItem("Segundo Grado");
                    comboDivision.addItem("Tercer Grado");
                    comboDivision.addItem("Cuarto Grado");
                    comboDivision.addItem("Quinto Grado");
                    comboDivision.addItem("Sexto Grado");
                    comboDivision.addItem("Septimo Grado");
                    labelGrado.setVisible(true);
                    LabelDivision.setVisible(true);
                    comboDivisionDelCurso.addItem("A");
                    comboDivisionDelCurso.addItem("B");
                    comboDivisionDelCurso.addItem("C");
                    comboDivisionDelCurso.addItem(" ");

                    }else if(seleccion.equals("Ingles")){
                    comboDivision.removeAllItems();
                    comboDivisionDelCurso.removeAllItems();
                    comboDivision.addItem("Seleccione..");
                    comboDivisionDelCurso.addItem("Seleccione..");
                    comboDivision.setVisible(true);
                    labelGrado.setVisible(true);
                    LabelDivision.setVisible(false);
                    comboDivision.addItem("Primer Nivel");
                    comboDivision.addItem("Segundo Nivel");
                    comboDivision.addItem("Tercer Nivel");
                    comboDivision.addItem("Cuarto Nivel");
                    comboDivision.addItem("Quinto Nivel");
                    comboDivision.addItem("Sexto Nivel");
                    comboDivisionDelCurso.setVisible(true);
                    comboDivisionDelCurso.addItem("1");
                    comboDivisionDelCurso.addItem("2");
                    comboDivisionDelCurso.addItem("3");
                     }else{
                        comboDivision.setVisible(false);
                        comboDivisionDelCurso.setVisible(false);
                        LabelDivision.setVisible(false);
                        labelGrado.setVisible(false);
                    }


    }//GEN-LAST:event_comboCursoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //ValidarqueSeHayaElegidoAlgo
        try{
        Alumno fulano=Validador.elegirAlumnoLista((Alumno)jList.getSelectedValue());
        this.setVisible(false);
        VentanaModificar ventM=new VentanaModificar(fulano);
        ventM.setVisible(true);
        this.dispose();
        }catch (miExcepcionLista ex) {
            System.out.println("No selecciono nada y quiso Modificar/eliminar");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void comboDivisionDelCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboDivisionDelCursoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboDivisionDelCursoActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Alumno fulano;
        try {
            fulano = Validador.elegirAlumnoLista((Alumno) jList.getSelectedValue());

        Object[] possibleValues = { "Aceptar", "Cancelar"};
        Object ValorSeleccion = JOptionPane.showConfirmDialog(null,
        "Esta seguro que desea eliminar a: "+fulano.toString(), "Confirmacion de seleccion",
       JOptionPane.YES_NO_OPTION);
        
        if(ValorSeleccion.equals(JOptionPane.YES_OPTION)){
         Eliminar.EliminarAlumno(fulano.getId());   
        }
        } catch (miExcepcionLista ex) {
            System.out.println("No selecciono nada y quiso Modificar/eliminar");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.setVisible(false);
        ventanaPrincipal ventM=new ventanaPrincipal();
        ventM.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaModificarEliminar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelDivision;
    private javax.swing.JComboBox comboCurso;
    private javax.swing.JComboBox comboDivision;
    private javax.swing.JComboBox comboDivisionDelCurso;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelGrado;
    private javax.swing.JTextField textApellido;
    private javax.swing.JTextField textNombre;
    // End of variables declaration//GEN-END:variables

}
